﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class SearchResults : System.Web.UI.Page
{
    ManageDatabase data;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (data == null)
        {
            data = new ManageDatabase();
        }
        if (!IsPostBack)
        {
            if (Request.QueryString["text"] != null) //&& Request.QueryString["type"] != null && Request.QueryString["subtype"] != null
            {
                try
                {
                    String text = Server.UrlDecode(Request.QueryString["text"].ToString());
                    //int type = Convert.ToInt32(Request.QueryString["text"]);
                    //int subtype = Convert.ToInt32(Request.QueryString["subtype"]);
                    DoSearch(text);
                }
                catch (Exception) {
                    DoSearch("");
                }
            }
            else
            {
                DoSearch("");
            }
        }
    }

    //private void Initialize(String text, int type, int subtype, Boolean IsSearchValid)
    //{
    //    if (IsSearchValid)
    //    {
    //        DoSearch(text);
    //    }
    //    else {
    //        DoSearch("");
    //    }
    //}

    private void DoSearch(String text)
    {
        Label ResultText = new Label();
        Table ResultTable = new Table();
        ResultTable.CssClass = "gradient-style";
        SearchFormText.Text = text;
        if (String.IsNullOrEmpty(text))
        {
            ResultText.Text = "A keresésnek nem adott vissza találatot!";
            SearchResultPanel.Controls.Add(ResultText);
            return;
        }
        data = new ManageDatabase();

        List<Search> result = data.Search(text);
        int i = 0;
        if (result != null)
        {
            TableHeaderRow ResultHeaderRow = new TableHeaderRow();
            TableHeaderCell ResultHeaderCell1 = new TableHeaderCell();
            TableHeaderCell ResultHeaderCell2 = new TableHeaderCell();
            Label Cell1 = new Label();
            Label Cell2 = new Label();
            Cell1.Text = "#";
            Cell2.Text = "Link a tartalomra";
            ResultHeaderCell1.Controls.Add(Cell1);
            ResultHeaderCell2.Controls.Add(Cell2);
            ResultHeaderRow.Controls.Add(ResultHeaderCell1);
            ResultHeaderRow.Controls.Add(ResultHeaderCell2);
            ResultTable.Controls.Add(ResultHeaderRow);
            foreach (Search item in result)
            {
                TableRow InfoRow = new TableRow();
                TableCell Info1Cell = new TableCell();
                TableCell Info2Cell = new TableCell();
                Label Count = new Label();
                HyperLink TitleHun = new HyperLink();

                i++;
                Count.Text = i.ToString() + ". ";
                TitleHun.Text = item.text;
                if (item.type.Equals("sorozat"))
                    TitleHun.NavigateUrl = "Seasons.aspx?sorozat=" + item.id.ToString();
                else if (item.type.Equals("evad"))
                    TitleHun.NavigateUrl = "Episodes.aspx?evad=" + item.id.ToString();
                else if (item.type.Equals("epizod"))
                    TitleHun.NavigateUrl = "ViewEpisode.aspx?epizod=" + item.id.ToString();

                Info1Cell.Controls.Add(Count);
                Info2Cell.Controls.Add(TitleHun);

                InfoRow.Controls.Add(Info1Cell);
                InfoRow.Controls.Add(Info2Cell);

                ResultTable.Controls.Add(InfoRow);
            }
        }
        ResultText.Text = "A keresésnek " + i.ToString() + " találatot adott vissza!";
        SearchResultPanel.Controls.Add(ResultText);
        SearchResultPanel.Controls.Add(ResultTable);
        
    }

    protected void SearchButton_Click(object sender, EventArgs e) {
        DoSearch(SearchFormText.Text);
    }
}